package com.ymatou.authapp.testcase;

import java.util.Date;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.authapp.parameter.UserTokenDtoBean;
import com.ymatou.authapp.service.UserTokenDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.utils.TestDataManager;
/**
*创建用户Token接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UserTokenDto{
	private static UserTokenDtoBean usertokendtoBean;
	private static UserTokenDtoCall usertokendtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建用户Token接口");
	}

	@Before
	public void caseUp() {
		usertokendtoBean=new UserTokenDtoBean();
		usertokendtoCall=new UserTokenDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(usertokendtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserTokenDto_001() {
		Logger.start(true,"用户不存在，创建用户token");
		try {
			int uId = getNewUid();
			usertokendtoBean.setUserId(uId);
			usertokendtoBean.setTokenTimeOut((new Date()).getTime());
			usertokendtoBean.setContext("Login");
			usertokendtoBean.setTokenSource("App");
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();			
			
			Logger.verifyEquals(true, usertokendtoCall.getSuccess(), "验证是否成功");
			Thread.sleep(100);
			Logger.verifyEquals(findUserByUid(uId).get("sId"), usertokendtoCall.getResult(), "验证token信息");
			Logger.comment("恢复数据，从数据库移除此token信息");
			deleteUserTokenByUserId(uId);
			Logger.verifyIsNull(findUserByUid(uId), "验证数据已被移除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserTokenDto_002() {
		Logger.start(false,"用户已存在，则直接返回token信息");
		try {
			Map<String, Object> userToken = findUserByUid(6209196);
			usertokendtoBean.setUserId(6209196);
			usertokendtoBean.setTokenTimeOut((new Date()).getTime());
			usertokendtoBean.setContext("Login");
			usertokendtoBean.setTokenSource("App");
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();			
			
			Logger.verifyEquals(true, usertokendtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals(userToken.get("sId"), usertokendtoCall.getResult(), "验证Token信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 生成新的UID
	 * @return
	 * @throws Exception
	 */
	private int getNewUid() throws Exception{
		int uId = Integer.valueOf(TestDataManager.getRandomNum(8));
		while(findUserByUid(uId)!=null){
			uId = Integer.valueOf(TestDataManager.getRandomNum(8));
		}
		return uId;
	}
	
	/**
	 * 根据UserID删除信息
	 * @param loginId
	 * @throws Exception 
	 */
	private void deleteUserTokenByUserId(int uid) throws Exception{
		UserTokenWapper wapper = new UserTokenWapper();
		wapper.deleteData("{\"uId\":" + uid + "}");
	}
	
	/**
	 * 判断用户是否存在
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	private Map<String, Object> findUserByUid(int uid) throws Exception{
		UserTokenWapper wapper = new UserTokenWapper();
		Map<String, Object> bean = wapper.getUserTokenByUid(uid);
		return bean;
	}
}